/**
 * Copyright (C), 2019-2020, **有限公司
 * FileName: SftpController
 * Author:   zhuzj29042
 * Date:     2020/2/14 15:54::31
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.caixin.data.middle.etl.kettle.admin.controller;

import com.caixin.data.middle.etl.kettle.ext.PluginFactory;
import com.caixin.data.middle.etl.kettle.ext.base.GraphCodec;
import com.caixin.data.middle.etl.kettle.ext.util.JsonUtils;
import org.pentaho.di.core.Const;
import org.pentaho.di.i18n.BaseMessages;
import org.pentaho.di.job.JobMeta;
import org.pentaho.di.job.entries.sftp.JobEntrySFTP;
import org.pentaho.di.job.entries.sftp.SFTPClient;
import org.pentaho.di.job.entry.JobEntryCopy;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.net.InetAddress;

/**
 * 〈一句话功能简述〉<br> 
 *
 *
 * @author zhuzhongji
 * @create 2020/2/14 15:54:31
 * @since 1.0.0
 */
@Controller
@RequestMapping(value="/sftp")
public class SftpController {

    @ResponseBody
    @RequestMapping(method= RequestMethod.POST, value="/test")
    protected void sftptest(@RequestParam String graphXml, @RequestParam String stepName) throws Exception {
        GraphCodec codec = (GraphCodec) PluginFactory.getBean(GraphCodec.JOB_CODEC);
        JobMeta jobMeta = (JobMeta) codec.decode(graphXml);

        JobEntryCopy jobEntryCopy = jobMeta.findJobEntry(stepName);
        JobEntrySFTP sftp = (JobEntrySFTP) jobEntryCopy.getEntry();

        String info = "";
        SFTPClient sftpclient = null;
        try {
            String servername = jobMeta.environmentSubstitute(sftp.getServerName());
            String serverport = jobMeta.environmentSubstitute(sftp.getServerPort());
            String username = jobMeta.environmentSubstitute(sftp.getUserName());
            String password = jobMeta.environmentSubstitute(sftp.getPassword());
            String keyFilename = jobMeta.environmentSubstitute(sftp.getKeyFilename());
            String keyFilePass = jobMeta.environmentSubstitute(sftp.getKeyPassPhrase());

            sftpclient = new SFTPClient(InetAddress.getByName(servername), Const.toInt(serverport, 22), username, keyFilename, keyFilePass);
            String proxyHost = jobMeta.environmentSubstitute(sftp.getProxyHost());
            String proxyPort = jobMeta.environmentSubstitute(sftp.getProxyPort());
            String proxyUsername = jobMeta.environmentSubstitute(sftp.getProxyUsername());
            String proxyPass = jobMeta.environmentSubstitute(sftp.getProxyPassword());
            String proxyType = jobMeta.environmentSubstitute(sftp.getProxyType());
            if (!Const.isEmpty(proxyHost)) {
                sftpclient.setProxy(proxyHost, proxyPort, proxyUsername, proxyPass, proxyType);
            }
            sftpclient.login(password);

            JsonUtils.success(BaseMessages.getString( JobEntrySFTP.class, "JobSFTP.Connected.Title.Ok" ),
                    BaseMessages.getString( JobEntrySFTP.class, "JobSFTP.Connected.OK", sftp.getServerName() ) + Const.CR);
            return;
        } catch (Exception e) {
            if (sftpclient != null) {
                try {
                    sftpclient.disconnect();
                } catch (Exception ignored) {
                }
                sftpclient = null;
            }
            info = e.getMessage();
        }

        JsonUtils.fail(BaseMessages.getString( JobEntrySFTP.class, "JobSFTP.ErrorConnect.Title.Bad" ),
                BaseMessages.getString( JobEntrySFTP.class, "JobSFTP.ErrorConnect.NOK", sftp.getServerName(), info) + Const.CR);
    }

    @ResponseBody
    @RequestMapping(method=RequestMethod.POST, value="/testdir")
    protected void sftpdirtest(@RequestParam String graphXml, @RequestParam String stepName) throws Exception {
        GraphCodec codec = (GraphCodec) PluginFactory.getBean(GraphCodec.JOB_CODEC);
        JobMeta jobMeta = (JobMeta) codec.decode(graphXml);

        JobEntryCopy jobEntryCopy = jobMeta.findJobEntry(stepName);
        JobEntrySFTP sftp = (JobEntrySFTP) jobEntryCopy.getEntry();

        String info = "";
        SFTPClient sftpclient = null;
        try {
            String servername = jobMeta.environmentSubstitute(sftp.getServerName());
            String serverport = jobMeta.environmentSubstitute(sftp.getServerPort());
            String username = jobMeta.environmentSubstitute(sftp.getUserName());
            String password = jobMeta.environmentSubstitute(sftp.getPassword());
            String keyFilename = jobMeta.environmentSubstitute(sftp.getKeyFilename());
            String keyFilePass = jobMeta.environmentSubstitute(sftp.getKeyPassPhrase());

            sftpclient = new SFTPClient(InetAddress.getByName(servername), Const.toInt(serverport, 22), username, keyFilename, keyFilePass);
            String proxyHost = jobMeta.environmentSubstitute(sftp.getProxyHost());
            String proxyPort = jobMeta.environmentSubstitute(sftp.getProxyPort());
            String proxyUsername = jobMeta.environmentSubstitute(sftp.getProxyUsername());
            String proxyPass = jobMeta.environmentSubstitute(sftp.getProxyPassword());
            String proxyType = jobMeta.environmentSubstitute(sftp.getProxyType());
            if (!Const.isEmpty(proxyHost)) {
                sftpclient.setProxy(proxyHost, proxyPort, proxyUsername, proxyPass, proxyType);
            }
            sftpclient.login(password);

            if(sftpclient.folderExists(sftp.getScpDirectory())) {
                JsonUtils.success(BaseMessages.getString( JobEntrySFTP.class, "JobSFTP.FolderExists.Title.Ok" ),
                        BaseMessages.getString( JobEntrySFTP.class, "JobSFTP.FolderExists.OK", sftp.getScpDirectory() ) + Const.CR);
                return;
            }
        } catch (Exception e) {
            if (sftpclient != null) {
                try {
                    sftpclient.disconnect();
                } catch (Exception ignored) {
                }
                sftpclient = null;
            }
            info = e.getMessage();
        }

        JsonUtils.fail(BaseMessages.getString( JobEntrySFTP.class, "JobSFTP.ErrorConnect.Title.Bad" ),
                BaseMessages.getString( JobEntrySFTP.class, "JobSFTP.ErrorConnect.NOK", sftp.getServerName(), info) + Const.CR);
    }

}
